close all;clear all;clc;
% Parameters
A = 1.1;
g = 2;
lambda = 0.2;
eta = 0.6;
gprime = 1;
k = 0.6;

% Functions
tauf = @(g, gprime, lambda, eta, A, k)...
    max(1/(g-gprime)*(log(A)-log((exp((g-gprime)*k*eta)-1)/((g-gprime)*eta)...
    + (1-k)*(g/lambda*exp((g-lambda-gprime)*k*eta)...
    - (g-lambda)/lambda*exp((g-gprime)*k*eta)))),0);

tauJf1 = @(g, gprime, lambda, eta, A, k, omega)...
    max(1/(g-gprime)*(log(A)...
    -log((1-omega)*(exp((g-gprime)*k*eta/(1-omega))-1)/((g-gprime)*eta)...
    + omega*exp((g-gprime)*k*eta/(1-omega))...
    + (1-omega-k)*(g/lambda*exp((g-lambda-gprime)*k*eta/(1-omega))...
    - (g-lambda)/lambda*exp((g-gprime)*k*eta/(1-omega))))),0);

tauJf2 = @(g, gprime, lambda, eta, A, k, omega)...
    1/(g-gprime)*(log(A) - log((exp((g-gprime)/lambda*log(g/(g-lambda)))...
    - (1-omega) - omega*exp((g-gprime)*eta/omega*(1/(lambda*eta)*log(g/(g-lambda))-k)))...
    /((g-gprime)*eta) + (omega*(1-k)+(1-omega)*(1/(lambda*eta)*log(g/(g-lambda))-k))...
    *(-(g-lambda)/lambda*exp((g-gprime)/lambda*log(g/(g-lambda)))...
    + g/lambda*exp((g-gprime-lambda)/lambda*log(g/(g-lambda))...
    + 1/omega*log(g/(g-lambda))-lambda*k*eta/omega))));

chif = @(g, gprime, lambda, eta, A, k, tau, d, omega)...
    (g-lambda)/lambda - g/lambda*exp(-lambda*(k*eta-(1-omega)*d))...
    + (A*exp(-(g-gprime)*(tau+omega*d+k*eta))...
    - (1 - (1-omega)*exp(-(g-gprime)*(omega*d+k*eta))...
    - omega*exp((g-gprime)*((1-omega)*d-k*eta)))/((g-gprime)*eta))...
    /(omega*(1-k)+(1-omega)*d*omega/eta);

rhof = @(g, gprime, lambda, eta, A, k, omega)...
    (A+(1-omega)/((g-gprime)*eta))*exp(-(g-gprime)*k*eta/(1-omega))...
    - (1-omega)/((g-gprime)*eta) - omega;

omegatildef = @(g, gprime, lambda, eta, A, k)...
    min(fsolve(@(x) rhof(g, gprime, lambda, eta, A, k, x), 0),...
    1 - k/(1/(lambda*eta)*log(g/(g-lambda))));

% Calculations
step = 1e-3;
omega = 0:step:1;

omegatilde = omegatildef(g, gprime, lambda, eta, A, k);

for i = 1:length(omega)
    % full repayment for senior creditors
    if omega(i) <= omegatilde
        tauS(i) = NaN;
        tauJ(i) = tauJf1(g, gprime, lambda, eta, A, k, omega(i));
        delta(i) = tauJ(i) + k*eta/(1-omega(i));
    else % partial repayment for senior creditors
        if chif(g, gprime, lambda, eta, A, k, 0, 0, omega(i)) <= 0
            tauJ(i) = 0;
            tauS(i) = 0;
        else
            if chif(g, gprime, lambda, eta, A, k, 0,...
                    eta/omega(i)*(1/(lambda*eta)*log(g/(g-lambda))-k), omega(i)) <= 0
                tauJ(i) = 0;
                tauS(i) = fsolve(@(x) chif(g, gprime, lambda, eta, A, k, 0, x, omega(i)), 0);
            else
                tauJ(i) = tauJf2(g, gprime, lambda, eta, A, k, omega(i));
                tauS(i) = tauJ(i) + eta/omega(i)*(1/(lambda*eta)*log(g/(g-lambda))-k);
            end
        end
        delta(i) = (1-omega(i))*tauJ(i) + omega(i)*tauS(i) + k*eta;
    end
end

aux1 = [omegatilde omegatilde];
tauS_omegatilde = fsolve(@(x)...
    chif(g, gprime, lambda, eta, A, k, 0, x, omegatilde), 0);

tau = tauf(g, gprime, lambda, eta, A, k);

% Points for figures
for j = 1:length(omega)
    if (j==1)|(j==length(omega))|(mod(j,100)==0)
        tauJ_figure(j) = tauJ(j);
        tauS_figure(j) = tauS(j);
    else
        tauJ_figure(j) = NaN;
        tauS_figure(j) = NaN;
    end
end

% Figures
h1 = figure;
set(h1,'defaultLegendInterpreter','latex',...
    'defaultAxesTickLabelInterpreter','latex');
a = axes;

yyaxis left;
p1 = plot(omega, tauS_figure, '-.k*','linewidth',1.5,'MarkerSize',8);
hold on;
p2 = plot(omega, tauJ_figure, '-.ok','linewidth',1.5,'MarkerSize',8);
plot(omega, tauS, '-.k','linewidth', 1.5, 'MarkerSize',8);
plot(omega, tauJ, '-.k','linewidth', 1.5, 'MarkerSize',8);

yl = ylim();
plot(aux1, [yl(1) tauS_omegatilde], 'k--', 'linewidth', 1);
plot([0 1], [tau tau], 'k--', 'linewidth', 1);

set(a, 'YColor', 'k', 'ylim', [yl(1)-0.001 yl(2)]);

yyaxis right;
set(a, 'ylim', [yl(1)-0.001 yl(2)], 'YColor', 'k',...
    'YTick',[tau], 'YTicklabel',{'$\tau^{*}$'},...
    'XTick',[0 omegatilde 1], 'XTicklabel',{'$0$' '$\tilde{\omega}$' '$1$'},...
    'Fontsize', 16);

xlabel('$\omega$', 'interpreter','latex');
legend([p1 p2], {'$\tau_S^{*}$' '$\tau_J^{*}$'});

%saveas(h1, 'tauJ-tauS-2.eps');
%--------------------------------------------------------------------------
h2 = figure;
a = axes;
set(h2, 'defaultLegendInterpreter','latex',...
    'defaultAxesTickLabelInterpreter','latex');

yyaxis left;
plot(omega, delta, 'k', 'linewidth', 1.5);
hold on;
yl2 = ylim();
plot(aux1, [yl2(1) omegatilde*tauS_omegatilde+k*eta], 'k--', 'linewidth', 1);
plot([0 1], [tau+k*eta tau+k*eta], 'k--', 'linewidth', 1);

set(a, 'ylim', [yl2(1) yl2(2)], 'YColor', 'k');

ylabel('$\hat{t}-t_0$', 'interpreter','latex');

yyaxis right;
set(a, 'XTick',[0 omegatilde 1],...
    'XTicklabel',{'$0$' '$\tilde{\omega}$' '$1$'},...
    'YTick',[tau+k*eta],...
    'YTicklabel',{'$\tau^{*}+k\eta$'}, 'ylim', [yl2(1) yl2(2)],...
    'YColor', 'k', 'TickLabelInterpreter', 'latex',...
    'Fontsize', 16);

xlabel('$\omega$', 'interpreter','latex');

%saveas(h2, 'Delta-omega-2.eps');
